package com.moonshineproject.plugin.actionscript.as3parser.parsers
{
	import org.as3commons.asblocks.parser.api.IParserNode;
	
	public class ExtensionParser
	{
		protected var ast:IParserNode;
		protected var imports:ImportParser;
		
		public var extendsClass:String;
		
		public function ExtensionParser(ast:IParserNode, imports:ImportParser)
		{
			this.ast = ast;
			this.imports = imports;
		}
		
		public function resolveExtend():void
		{
			extendsClass = findExtend(ast);

			if (extendsClass)
			{
				var packageName:String = imports.getPackageNameForType(extendsClass);
				if (packageName)
				{
					extendsClass = packageName + "." + extendsClass;
				}
			}
		}
		
		protected function findExtend(searchNode:IParserNode):String
		{
			for each (var node:IParserNode in searchNode.children)
			{
				if (node.kind == "package")
				{
					return findExtend(node);
				}
				else if (node.kind == "class")
				{
					return findExtend(node);
				}
				else if (node.kind == "content")
				{
					return findExtend(node);
				}
				else if (node.kind == "extends")
				{
					return getExtends(node);
				}
			}
			
			return null;
		}
			
		protected function getExtends(extendsNode:IParserNode):String
		{
			for each (var node:IParserNode in extendsNode.children)
			{
				if (node.kind == "type")
				{
					return node.stringValue;
				}
			}
			
			return null;
		}
	}
}